package me.zhengjie.modules.xiao.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.annotation.rest.AnonymousGetMapping;
import me.zhengjie.modules.xiao.domain.Swiper;
import me.zhengjie.modules.xiao.domain.vo.SwiperQueryCriteria;
import me.zhengjie.modules.xiao.service.SwiperService;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.Result;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* @author cg
* @date 2025-04-09
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "轮播图管理")
@RequestMapping("/api/swiper")
public class SwiperController {

    private final SwiperService swiperService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('swiper:list')")
    public void exportSwiper(HttpServletResponse response, SwiperQueryCriteria criteria) throws IOException {
        swiperService.download(swiperService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询轮播图")
    @ApiOperation("查询轮播图")
    @PreAuthorize("@el.check('swiper:list')")
    public ResponseEntity<PageResult<Swiper>> querySwiper(SwiperQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(swiperService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增轮播图")
    @ApiOperation("新增轮播图")
    @PreAuthorize("@el.check('swiper:add')")
    public ResponseEntity<Object> createSwiper(@Validated @RequestBody Swiper resources){
        swiperService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改轮播图")
    @ApiOperation("修改轮播图")
    @PreAuthorize("@el.check('swiper:edit')")
    public ResponseEntity<Object> updateSwiper(@Validated @RequestBody Swiper resources){
        swiperService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除轮播图")
    @ApiOperation("删除轮播图")
    @PreAuthorize("@el.check('swiper:del')")
    public ResponseEntity<Object> deleteSwiper(@RequestBody List<Integer> ids) {
        swiperService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("查询轮播图API")
    @ApiOperation("查询轮播图API")
    @AnonymousGetMapping(value = "/list")
    public Result<List<Swiper>> list(SwiperQueryCriteria criteria){
        criteria.setType("1");
        return Result.success(swiperService.queryAll(criteria));
    }

    @Log("查询预约轮播图API")
    @ApiOperation("查询预约轮播图API")
    @AnonymousGetMapping(value = "/listYY")
    public Result<Swiper> listYY(SwiperQueryCriteria criteria){
        criteria.setType("2");
        List<Swiper> swiperList = swiperService.queryAll(criteria);
        if(null != swiperList && swiperList.size() != 0){
            return Result.success(swiperList.get(0));
        }else{
            return Result.fail("500", "未获取到预约轮播图");
        }

    }

    @Log("查询预约轮播图API")
    @ApiOperation("查询预约轮播图API")
    @AnonymousGetMapping(value = "/listJGYG")
    public Result<Swiper> listJGYG(SwiperQueryCriteria criteria){
        criteria.setType("4");
        List<Swiper> swiperList = swiperService.queryAll(criteria);
        if(null != swiperList && swiperList.size() != 0){
            return Result.success(swiperList.get(0));
        }else{
            return Result.fail("500", "未获取到预约轮播图");
        }

    }
}